using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Text;
using System.Web.SessionState;

namespace Microsoft.ACEEngineering.SPM.ErrorHandling
{
    /// <summary>
    /// Class to handle error logging for the website.
    /// </summary>
    public class LogManager
    {
        public LogManager()
        {
            
        }
        /// <summary>
        /// This method will log exceptions into the event log.
        /// </summary>
        /// <param name="exceptionObject"></param>
        /// <param name="httpSessionStateobject"></param>
        /// <param name="httpRequestobject"></param>
        public  void LogErrorData(Exception exceptionObject, HttpSessionState httpSessionStateobject, HttpRequest httpRequestobject)
        {

            using (EventLog objEventLog = new EventLog())
            {
                String eventLogName = System.Configuration.ConfigurationManager.AppSettings["EventLogName"].ToString().Trim();

                try
                {
                    if (!EventLog.SourceExists(eventLogName))
                    {
                        EventLog.CreateEventSource(eventLogName, eventLogName);
                    }

                    objEventLog.Source = eventLogName;

                    objEventLog.WriteEntry(GetErrorDetails(exceptionObject, httpSessionStateobject, httpRequestobject).ToString(), EventLogEntryType.Error, 1000);
                }
                catch
                {
                    //throw exceptionObject; //unfortunately the write to the event log has failed (it may be full). No action for this cases.
                    return;

                }
            }
        }
        /// <summary>
        /// This method returns all the details by iterating through all the Inner exceptions.
        /// </summary>
        /// <param name="exobject"></param>
        /// <param name="httpSessionStateobject"></param>
        /// <param name="httpRequestobject"></param>
        /// <returns>Detailed string with all error information.</returns>
        private  StringBuilder GetErrorDetails(Exception exobject, HttpSessionState httpSessionStateobject, HttpRequest httpRequestobject)
        {
            StringBuilder errorStack = new StringBuilder();

            // SessionID information
            errorStack.Append("Session ID - " + httpSessionStateobject.SessionID.ToString() + "\n");
            errorStack.Append("IsCookieless - " + httpSessionStateobject.IsCookieless.ToString() + "\n \n");
            errorStack.Append("Request URL - " + httpRequestobject.Path.ToString() + "\n \n");

            //Iterate all inner exception.
            while (exobject != null)
            {
                errorStack.Append("Exception Type:" + exobject.GetType().ToString() + "\n");
                errorStack.Append("Error Message:" + exobject.Message.ToString() + "\n");
                errorStack.Append("Error Source:" + exobject.Source.ToString() + "\n");
                errorStack.Append("Target Site:" + exobject.TargetSite.ToString() + "\n");
                errorStack.Append("Stack Trace:" + exobject.StackTrace.ToString() + "\n \n");
                exobject = exobject.InnerException;
            }
            return errorStack;
        }
    }
}
